/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */
package menu;

import java.util.ArrayList;

import menu.MenuButton.ButtonType;
import menu.tabobject.MenuTabObject;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import sq.FontLoader;
import sq.RPG;

/** A Menu Tab used in a SubMenu
 *
 */
public class MenuTab {

    private static int TITLE_TOP = 110;
    private String title;
    private ArrayList<MenuTabObject> tabObjects;
    private ButtonType menuState;

    /** Create a new MenuTab object
     * @param tabObjects a list of tab objects
     * @param title The title of the menu
     */
    public MenuTab(ArrayList<MenuTabObject> tabObjects, String title)
            throws SlickException {

        this.setTabObjects(tabObjects);
        this.setTitle(title);
        this.setMenuState(ButtonType.NONE);

    }

    /** Update the menu state for a frame.
     * @param input The input object
     * @param delta Time passed since last frame (milliseconds).
     */
    public void update(Input input, int delta)
            throws SlickException {

        for (MenuTabObject o : this.getTabObjects()) {
            o.update(input, delta);
        }

    }

    /** Render the entire screen, so it reflects the current game state.
     * @param g The Slick graphics object, used for drawing.
     */
    public void render(Graphics g)
            throws SlickException {

        g.setColor(FontLoader.getTitleColor());
        g.setFont(FontLoader.getFontTitle25());
        String text = this.getTitle();
        int width = g.getFont().getWidth(text);
        int x = (RPG.getScreenWidth() - width) / 2;
        g.drawString(text, x, TITLE_TOP);

        for (MenuTabObject o : this.getTabObjects()) {
            o.render(g);
        }

    }

    public ButtonType getMenuState() {
        return this.menuState;
    }

    public void setMenuState(ButtonType menuState) {
        this.menuState = menuState;
    }

    public ArrayList<MenuTabObject> getTabObjects() {
        return tabObjects;
    }

    public void setTabObjects(ArrayList<MenuTabObject> tabObjects) {
        this.tabObjects = tabObjects;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void resetTab() {

        for (MenuTabObject o : this.getTabObjects()) {
            o.resetValue();
        }

    }

    public void saveObjectValues() {

        for (MenuTabObject o : this.getTabObjects()) {
            o.saveValue();
        }

    }
}
